from datetime import datetime
from enum import Enum
from typing import List, Optional, Dict, Any
import uuid
from dataclasses import dataclass, field
import json


class UserRole(Enum):
    ADMIN = "admin"
    TEACHER = "teacher"
    ASSISTANT = "assistant"


class UserStatus(Enum):
    ACTIVE = "active"
    INACTIVE = "inactive"
    SUSPENDED = "suspended"


class QuestionType(Enum):
    MULTIPLE_CHOICE = "multiple_choice"
    TRUE_FALSE = "true_false"
    FILL_BLANK = "fill_blank"
    SHORT_ANSWER = "short_answer"
    ESSAY = "essay"


class DifficultyLevel(Enum):
    EASY = 1
    MEDIUM = 2
    HARD = 3
    VERY_HARD = 4


class PaperStatus(Enum):
    DRAFT = "draft"
    PENDING_REVIEW = "pending_review"
    PUBLISHED = "published"
    IN_USE = "in_use"
    ARCHIVED = "archived"


@dataclass
class User:
    id: str = field(default_factory=lambda: str(uuid.uuid4()))
    username: str = ""
    password: str = ""
    email: str = ""
    role: UserRole = UserRole.ASSISTANT
    status: UserStatus = UserStatus.ACTIVE
    created_time: datetime = field(default_factory=datetime.now)
    last_login: Optional[datetime] = None

    def login(self, password: str) -> bool:
        """用户登录验证"""
        if self.password == password and self.status == UserStatus.ACTIVE:
            self.last_login = datetime.now()
            return True
        return False

    def logout(self):
        """用户登出"""
        pass


@dataclass
class Administrator(User):
    def __post_init__(self):
        self.role = UserRole.ADMIN

    def manage_user(self, action: str, user_data: Dict) -> bool:
        """管理用户账户"""
        # 实现用户管理逻辑
        pass


@dataclass
class Teacher(User):
    subjects: List[str] = field(default_factory=list)

    def __post_init__(self):
        self.role = UserRole.TEACHER

    def create_manual_paper(self, questions: List[str]) -> 'Paper':
        """手动创建试卷"""
        pass


@dataclass
class Assistant(User):
    def __post_init__(self):
        self.role = UserRole.ASSISTANT


@dataclass
class KnowledgePoint:
    id: str = field(default_factory=lambda: str(uuid.uuid4()))
    name: str = ""
    subject: str = ""
    chapter: str = ""
    description: str = ""


@dataclass
class Option:
    id: str = field(default_factory=lambda: str(uuid.uuid4()))
    content: str = ""
    is_correct: bool = False


@dataclass
class Question:
    id: str = field(default_factory=lambda: str(uuid.uuid4()))
    content: str = ""
    question_type: QuestionType = QuestionType.MULTIPLE_CHOICE
    options: List[Option] = field(default_factory=list)
    answer: str = ""
    explanation: str = ""
    difficulty: DifficultyLevel = DifficultyLevel.MEDIUM
    difficulty_coefficient: float = 0.5
    knowledge_points: List[KnowledgePoint] = field(default_factory=list)
    subject: str = ""
    chapter: str = ""
    tags: List[str] = field(default_factory=list)
    creator_id: str = ""
    created_time: datetime = field(default_factory=datetime.now)
    last_modified: datetime = field(default_factory=datetime.now)
    usage_count: int = 0


@dataclass
class UsageRecord:
    id: str = field(default_factory=lambda: str(uuid.uuid4()))
    question_id: str = ""
    exam_date: datetime = field(default_factory=datetime.now)
    paper_id: str = ""
    frequency: int = 1


@dataclass
class PaperQuestionLink:
    paper_id: str = ""
    question_id: str = ""
    order: int = 0
    score: float = 0.0


@dataclass
class Paper:
    id: str = field(default_factory=lambda: str(uuid.uuid4()))
    title: str = ""
    description: str = ""
    creator_id: str = ""
    created_time: datetime = field(default_factory=datetime.now)
    total_score: float = 0.0
    status: PaperStatus = PaperStatus.DRAFT
    questions: List[PaperQuestionLink] = field(default_factory=list)
    subject: str = ""
    estimated_time: int = 0  # 预计完成时间（分钟）


@dataclass
class LogEntry:
    id: str = field(default_factory=lambda: str(uuid.uuid4()))
    user_id: str = ""
    action: str = ""
    details: str = ""
    timestamp: datetime = field(default_factory=datetime.now)
    ip_address: str = ""


@dataclass
class AnalysisReport:
    id: str = field(default_factory=lambda: str(uuid.uuid4()))
    report_type: str = ""  # difficulty, usage, similarity
    target_id: str = ""  # question_id or paper_id
    content: Dict[str, Any] = field(default_factory=dict)
    generated_time: datetime = field(default_factory=datetime.now)
    generated_by: str = ""

@dataclass
class ExamRecord:
    """考察记录模型"""
    id: str = field(default_factory=lambda: str(uuid.uuid4()))
    question_id: str = ""
    exam_date: datetime = field(default_factory=datetime.now)
    exam_name: str = ""
    paper_id: str = ""
    subject: str = ""
    chapter: str = ""
    knowledge_points: List[str] = field(default_factory=list)
    created_time: datetime = field(default_factory=datetime.now)

@dataclass
class SimilarityResult:
    """相似度结果模型"""
    id: str = field(default_factory=lambda: str(uuid.uuid4()))
    question_id: str = ""
    similar_question_id: str = ""
    similarity_score: float = 0.0
    algorithm_used: str = "jaccard"
    created_time: datetime = field(default_factory=datetime.now)

@dataclass
class DifficultyAnalysis:
    """难度分析结果模型"""
    id: str = field(default_factory=lambda: str(uuid.uuid4()))
    question_id: str = ""
    predicted_difficulty: int = 2
    actual_difficulty: int = 2
    confidence: float = 0.5
    analysis_factors: Dict[str, Any] = field(default_factory=dict)
    created_time: datetime = field(default_factory=datetime.now)